# -*- coding:utf-8 -*-

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from process_bar import ProgressBar
import datetime
import sys
import requests
import re
import json
import multiprocessing
import time

key_words = [u'面膜', u'洗面奶', u'花露水', u'化妆棉', u'眼霜', u'杏仁', u'饼干', u'薯片', u'麦片', u'牛肉干', u'巧克力', u'红酒',
             u'伏特加', u'金酒', u'茅台', u'啤酒', u'牛奶', u'鸡蛋', u'猪肋骨', u'鱼', u'土鸡', u'牛排', u'苹果', u'橙子', u'速冻水饺',
             u'NFC果汁', u'玉米油', u'米', u'盐', u'糖', u'酱油', u'醋', u'卷纸', u'抽纸', u'沐浴露', u'洗发水', u'洗洁精', u'洗衣粉',
             u'香皂', u'保鲜膜', u'保鲜袋', u'卫生巾', u'牙膏', u'牙刷', u'毛巾']
res = [0] * len(key_words)
pool = multiprocessing.Pool(processes=10)


def search_key_word(driver, key_word, index):
    # try:
    wait = WebDriverWait(driver, 10)
    wait.until(EC.presence_of_element_located((By.TAG_NAME, 'iframe')))
    iframe = driver.find_element_by_tag_name('iframe')
    # print len(iframe)
    driver.switch_to_frame(iframe)
    try:
        elem_name = driver.find_element_by_name('TPL_username')
        elem_name.clear()
        elem_name.send_keys(u'追梦者梦在心中')
        elem_password = driver.find_element_by_name('TPL_password')
        elem_password.clear()
        elem_password.send_keys('19960221ykwan')
        driver.find_element_by_id('btn-submit').click()
    except Exception, e:
        pass
    driver.switch_to_default_content()
    # except Exception, e:
    #     print e.message
    print key_word
    process_bar = None
    global count_num
    elem_search = driver.find_element_by_name('q')
    elem_search.clear()
    elem_search.send_keys(key_word)
    elem_search.send_keys(Keys.RETURN)
    while True:
        if driver.title == u'理想生活上天猫':
            iframe = driver.find_element_by_tag_name('iframe')
            driver.switch_to_frame(iframe)
            elem_switch = driver.find_element_by_class_name('login-switch')
            elem_switch.click()
            elem_name = driver.find_element_by_name('TPL_username')
            elem_name.clear()
            elem_name.send_keys(u'追梦者梦在心中')
            elem_password = driver.find_element_by_name('TPL_password')
            elem_password.clear()
            elem_password.send_keys('19960221ykwan')
            elem_login = driver.find_element_by_id('J_SubmitStatic')
            elem_login.click()
            # driver.switch_to.parent_frame()
            # print driver.page_source
            driver.implicitly_wait(2)
        elif u'天猫超市-天猫Tmall.com-上天猫，就购了-理想生活上天猫' not in driver.title:
            wait = WebDriverWait(driver, 3600 * 24)
            element = driver.find_element_by_tag_name('div')
            ActionChains(driver).context_click(element).perform()
            elem = wait.until(EC.title_contains(u'天猫超市-天猫Tmall.com-上天猫，就购了-理想生活上天猫'))
        try:
            elem_list = driver.find_element_by_id('J_ProductList')
            elem_products = elem_list.find_elements_by_class_name('product')
        except Exception, e:
            break
        for index_i, elem_product in enumerate(elem_products):
            # try:
            #     count_num = elem_product.find_element_by_class_name('item-sum').find_element_by_tag_name('strong').text
            #     if u'万' in count_num:
            #         count_num = float(count_num.replace(u'万', '')) * 10000
            #     res[index] += int(count_num)
            #     # print count_num
            # except Exception, e:
            #     # print elem_product.text
            #     print 'nothing'
            #     print elem_product.text

            while True:
                try:
                    count_num = get_comment_number_by_sku_and_store((elem_product.get_attribute('data-itemid'), 1))
                    print count_num, '=========', elem_product.get_attribute('data-itemid')
                    if count_num is None:
                        continue
                    else:
                        res[index] += int(count_num)
                        break
                except Exception, e:
                    print e.message
                    print elem_product.get_attribute('data-itemid')
            # try:
            #     s_s_t = (elem_product.get_attribute('data-itemid'), 1)
            # except Exception, e:
            #     continue
            # pool.apply_async(multi_get, (s_s_t, index, ))
        # time.sleep(2)
        try:
            elem_quick_page_changer = driver.find_element_by_class_name('quick-page-changer')
            elem_page_info = elem_quick_page_changer.find_element_by_tag_name('span').text.split('/')
            if len(elem_page_info) == 2:
                if process_bar is None:
                    process_bar = ProgressBar(count=int(elem_page_info[0]), total=int(elem_page_info[1]))
                else:
                    process_bar.move()
                process_bar.log()
                if elem_page_info[0] == elem_page_info[1]:
                    break
            elif len(elem_page_info) == 1:
                break
            elem = driver.find_element_by_class_name('page-next')
            try:
                # print elem.text, 'ssssssss'
                elem.click()
            except Exception, e:
                print e.message
                wait = WebDriverWait(driver, 3600 * 24)
                ActionChains(driver).context_click(elem).perform()
                wait.until(EC.element_to_be_clickable(elem))
                elem.click()

        except Exception, e:
            print e.message
            # print 'error->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->'
            break


def multi_get(sku_store_tuple, index):
    while True:
        try:
            count_num = get_comment_number_by_sku_and_store(sku_store_tuple)
            print count_num, '=========', sku_store_tuple[0]
            if count_num is None:
                continue
            else:
                res[index] += int(count_num)
                break
        except Exception, e:
            print e.message
            print sku_store_tuple


def get_comment_number_by_sku_and_store(sku_store_tuple):
    url = 'https://rate.tmall.com/list_detail_rate.htm?itemId={item_id}&sellerId={seller_id}&currentPage=1&content=0'
    response = requests.get(url.format(item_id=sku_store_tuple[0], seller_id=sku_store_tuple[1]))
    s = response.text.find('\"total\"')
    total_str = str((response.text)[s: s + 20])
    pattern = re.compile(r'"total":(\d+),')
    match = pattern.match(total_str)
    if match:
        return match.group(1)


def write_res():
    reload(sys)
    sys.setdefaultencoding('gbk')
    date_now = datetime.datetime.now().strftime('../data/TMALL_SUPERMARKET_%Y%m%d_%H%M%S.csv')
    file_out = open(date_now, 'w')
    file_out.write(','.join(key_words) + '\n')
    file_out.write(','.join([str(i) for i in res]) + '\n')
    file_out.close()


if __name__ == '__main__':
    init_driver = webdriver.Chrome()
    init_driver.get('https://chaoshi.tmall.com/')
    init_driver.maximize_window()
    for index, key in enumerate(key_words):
        search_key_word(init_driver, key, index)
    init_driver.quit()
    pool.close()
    pool.join()
    write_res()
